﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
public partial class account_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // 首先检查保存用户邮箱和密码的cookie是否存在，存在登录，不存在跳回登录页
        if (Request.Cookies["NOAP_USER_ACCOUNT"] != null)
        {
            HttpCookie UserAccountCookie = Request.Cookies["NOAP_USER_ACCOUNT"];
            string email = Convert.ToString(UserAccountCookie.Values["NOAP_USER_ACCOUNT_EMAIL"]);
            string password = Convert.ToString(UserAccountCookie.Values["NOAP_USER_ACCOUNT_PASSWORD"]);
            Session["email"] = email;
            Session["password"] = password;
            Response.Redirect("~/admin/Default.aspx");
        }

        // 有session,跳回主页
        if (Session["email"] != null && Session["password"] != null)
        {
            Response.Redirect("~/admin/Default.aspx");
        }
    }

    protected void login(object sender, EventArgs e)
    {
        string email = TextBox1.Text.Trim();
        string password = Convert.ToString(TextBox2.Text).Trim();
        string check = TextBox3.Text;

        string ConnectionString = "Server=.;Initial Catalog=noap;trusted_connection=true";
        SqlConnection objCon = new SqlConnection(ConnectionString);
        objCon.Open();
        // 开启数据库

        string checkSql = "select * from users where email = '" + email + "'";
        SqlCommand checkCom = new SqlCommand(checkSql, objCon);
        SqlDataReader res = checkCom.ExecuteReader();
        // 检查用户输入的数据

        if (res.Read())
        {
            string md5Password = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5");
            // 加密密码

            if (md5Password == Convert.ToString(res["password"]).Trim())
            {
                if (check == "on")
                {
                    HttpCookie UserAccountCookie = new HttpCookie("NOAP_USER_ACCOUNT");
                    UserAccountCookie["NOAP_USER_ACCOUNT_EMAIL"] = email;
                    UserAccountCookie["NOAP_USER_ACCOUNT_PASSWORD"] = md5Password;
                    UserAccountCookie.Expires = DateTime.Now.AddDays(2);
                    Response.Cookies.Add(UserAccountCookie);
                    // 将密码、邮箱储存至cookie,设置从现在开始两天后过期

                    Session["email"] = email;
                    Session["password"] = md5Password;
                    Response.Redirect("~/admin/Default.aspx");
                    // 成功登入 - 此时cookie存储
                }
                else
                {
                    Session["email"] = email;
                    Session["password"] = md5Password;
                    Response.Redirect("~/admin/Default.aspx");
                    // 成功登入 - 此时没有cookie存储
                }
            }
            else
            {
                Label1.Text = "用户密码错误！";
            }
        }
        else
        {
            Label1.Text = "用户不存在！";
        }
    }
}